package org.finmanager.statistics;

import com.alee.extended.panel.WebButtonGroup;
import com.alee.laf.button.WebButton;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StackedBarRenderer;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.data.category.CategoryDataset;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import static org.finmanager.core.Constants.DATE_FORMAT_MONTH;

/**
 * Created by Kelvin on 28/4/2014.
 */

//The UI of the statistic  bar chart function
public class CategoryStackedBarChartUI {
	JPanel mainPanel = new JPanel(new BorderLayout(0, 0));

	//Buttons to allow user to go to current/next/previous month in the middle on top of the chart
	WebButton increaseMonthButton = new WebButton(">");
	WebButton decreaseMonthButton = new WebButton("<");
	WebButton setCurrentMonthButton = new WebButton();

	public CategoryStackedBarChartUI(CategoryStackedBarChartController categoryStackedBarChartController) {
		increaseMonthButton.setActionCommand(CategoryStackedBarChartController.INCREASE_MONTH);
		decreaseMonthButton.setActionCommand(CategoryStackedBarChartController.DECREASE_MONTH);
		setCurrentMonthButton.setActionCommand(CategoryStackedBarChartController.SET_CURRENT_MONTH);
		WebButtonGroup controlButtonGroup = new WebButtonGroup(decreaseMonthButton, setCurrentMonthButton, increaseMonthButton);
		controlButtonGroup.setButtonsDrawFocus(false);
		JPanel controlPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		controlPanel.add(controlButtonGroup);

		ChartPanel chartPanel = new ChartPanel(createStackedBarChart(categoryStackedBarChartController.getCategoryStackedBarChartDataSet()));
		mainPanel.add(controlPanel, BorderLayout.NORTH);
		mainPanel.add(chartPanel, BorderLayout.CENTER);

		setCurrentMonthText(Calendar.getInstance());
	}

	public void addButtonListener(ActionListener listener) {
		increaseMonthButton.addActionListener(listener);
		decreaseMonthButton.addActionListener(listener);
		setCurrentMonthButton.addActionListener(listener);
	}

	//showing the month-year inside the button
	public void setCurrentMonthText(Calendar calendar) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_MONTH);
		setCurrentMonthButton.setText(dateFormat.format(calendar.getTime()));
	}

	//Manipulating the items to create the bar chart
	private JFreeChart createStackedBarChart(CategoryDataset dataSet) {
		JFreeChart chart = ChartFactory.createStackedBarChart(null, null, null, dataSet, PlotOrientation.VERTICAL, true, true, false);
		// TODO:
		// chart.setBackgroundPaint(getMainComponent().getBackground());

		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		plot.setForegroundAlpha(0.5f);
		plot.setNoDataMessage("NO DATA");

		StackedBarRenderer renderer = (StackedBarRenderer) plot.getRenderer();
		renderer.setRenderAsPercentages(true);
		renderer.setBarPainter(new StandardBarPainter());

		return chart;
	}

	public JComponent getMainComponent() {
		return mainPanel;
	}
}
